﻿using System;
using System.ServiceModel;

namespace Quebec.Silverlight.Utilities
{
    public class ProductLicense
    {
        public static void ValidateLicense(int productID, Guid productKey, Action<bool> callback)
        {
            BasicHttpBinding binding = new BasicHttpBinding();
            EndpointAddress endpoint = new EndpointAddress("http://licenses.cloudapp.net/LicenseService.svc");

            ILicenseService service =
                new ChannelFactory<ILicenseService>(binding, endpoint).CreateChannel();

            AsyncCallback asyncCallBack = delegate(IAsyncResult result)
            {
                var isLicenseValid = ((ILicenseService)result.AsyncState).EndValidateLicense(result);
                callback.Invoke(isLicenseValid);
            };

            service.BeginValidateLicense(productID, productKey, asyncCallBack, service);
        }
    }
}
